/**
 * 优先级队列
 * 1. 优先级队列,在插入一个元素的时候会考虑该数据的优先级
 */
class PriorityQueue {
  constructor() {
    this.items = [];
  }

  enqueue(element, priority) {
    const el = new PriorityQueue.QueueElement(element, priority);
    if (this.isEmpty()) {
      this.items.push(el);
    } else {
      let added = false;
      for (let i = 0; i < this.size(); i++) {
        if (priority < this.items[i].priority) {
          this.items[i].splice(i, 0, el);
          added = true;
          break;
        }
      }
      if (!added) {
        this.items.push(el);
      }
    }
  }

  //队列的长度
  size() {
    return this.items.length;
  }
  //查看队列是否为空
  isEmpty() {
    return this.size() == 0;
  }
  //查看队列中的所有元素
  toString() {
    return this.items.reduce((acc, curr) => (acc += curr + " "), "");
  }
}

PriorityQueue.QueueElement = class {
  constructor(element, priority) {
    this.element = element;
    this.priority = priority;
  }
};
